package cn.hesion.dataStructures.Tree;

public class TreeNode {
    private int no;
    private String name;
    private TreeNode right;
    private TreeNode left;

    public TreeNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public TreeNode getRight() {
        return right;
    }

    public void setRight(TreeNode right) {
        this.right = right;
    }

    public TreeNode getLeft() {
        return left;
    }

    public void setLeft(TreeNode left) {
        this.left = left;
    }

    @Override
    public String toString() {
        return "TreeNode[" +
                "no=" + no +
                ", name='" + name + '\'' +
                ']';
    }

    public void preOrder() {
        //先输出根节点
        System.out.println(this);
        //左节点不为空
        if (this.left != null) {
            this.left.preOrder();
        }
        //左节点不为空
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    public TreeNode preOrderSearch(int no) {
        TreeNode resNode=null;
        //前序遍历
        //如果根节点就是我们要找的节点
        //根
        if (this.no == no) {
            return this;
        }
        //如果根不是我们要找的则往左节点寻找
        //左节点不为空
        if(this.left!=null){
            resNode = this.left.preOrderSearch(no);
        }
        if(resNode != null){
            return resNode;
        }
        if(this.right !=null){
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;

    }
}
